//
//  PaperModuleTitleView.m
//  智云题库
//
//  Created by 毕武侠 on 2017/4/5.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperModuleTitleView.h"
#import "PaperModuleModel.h"
#import "NSString+Size.h"


@interface PaperModuleTitleView ()

@property (nonatomic, strong) UIView *lineView;

@end

@implementation PaperModuleTitleView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfigureView];
    }
    return self;
}

- (void)initConfigureView {
    self.backgroundColor = [UIColor whiteColor];
    self.showsHorizontalScrollIndicator = NO;
    self.index = 0;
}

- (void)setDatas:(NSMutableArray *)datas {
    _datas = datas;
    [self setupConfigureView];
}

- (void)setupConfigureView {
    if (_datas.count == 0) {
        return;
    }
    
    CGFloat edge = 8.0f;
    __block CGFloat x = 8.0f;
    [_datas enumerateObjectsUsingBlock:^(PaperModuleModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
        CGFloat w = [model.abbreviations textSizeIn:CGSizeMake(200, 40) font:[UIFont systemFontOfSize:14.0f]].width;
//        CGFloat width = edge + model.abbreviations.length * 4.0 + edge;
        CGFloat width = w + edge*2;
        if (width < SCREEN_WIDTH / 5) {
            width = SCREEN_WIDTH / 5;
        }
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, width, 40)];
        btn.tag = 10 + idx;
        [btn setTitle:model.abbreviations forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        x = btn.right;
    }];
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 39, 12, 1)];
    self.lineView.backgroundColor = MainThemeColor;
    [self addSubview:self.lineView];
    
    self.contentSize = CGSizeMake(x, 40);
    
    UIButton *btn = [self viewWithTag:10];
    [btn setTitleColor:MainThemeColor forState:UIControlStateNormal];
    self.lineView.left = btn.left + edge;
    self.lineView.width = btn.width - 2 * edge;
}

- (void)setIndex:(NSUInteger)index {
    if (_index == index) {
        return;
    }
    
    UIButton *lastBtn = [self viewWithTag:_index + 10];
    [lastBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];

    
    UIButton *newBtn = [self viewWithTag:index + 10];
    [newBtn setTitleColor:MainThemeColor forState:UIControlStateNormal];

    self.lineView.left = newBtn.left + 8.0;
    self.lineView.width = newBtn.width - 2 * 8.0;
    
    _index = index;
}

- (void)btnAction:(UIButton *)btn {
    if ([self.selectDelegate respondsToSelector:@selector(didSelectAtIndex:)]) {
        [self.selectDelegate didSelectAtIndex:btn.tag - 10];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
